var _max = 0;
function init(){
	_max = 400%25>0?0-(0-parseInt(400/25)-1):parseInt(400/25);
}
var _max_length = 0;
var _sn = 0;
function add_merchandise(id){
	var url = 'merchandise_list.html?'+id;
	var config = 'dialogWidth:810px;dialogHeight:565px;scroll:no;status:no;';
	var merchandise_list = window.showModalDialog(url,'',config);
	merchandise_list = merchandise_list==null?'':merchandise_list;
	//=========================
	// 删除之前存在的 foot 
	var foot = document.getElementById('foot');
	if(null!=foot&&merchandise_list.length>0){
		foot.removeNode(true);
	}
	//=========================
	_max_length =_max_length - merchandise_list.length;
	var is_less = 0-_max_length<_max;
	for(var i=0;i<merchandise_list.length;i++){
		var merchandise_info = merchandise_list[i].split('?');
		var merchandise_list_html = '';
		var style_border1 = !is_less&&i==merchandise_list.length-1?'border-bottom:0;':'';
		var style_border2 = i==0&&_sn>0?' border-top:1px solid #888;':'';
		var style_border = 'style="' + style_border1 + style_border2 + '"';
		merchandise_list_html = merchandise_list_html + '<tr style="height:25px;" onmouseover="cbgcolor(this,0)" onmouseout="cbgcolor(this,1)">';
		merchandise_list_html = merchandise_list_html + '  <td class="list" ' + style_border + ' width="200"><input type="hidden" name="merchandise_id" value="' + merchandise_info[0] + '" />' + merchandise_info[1] + '</td>';
		merchandise_list_html = merchandise_list_html + '  <td class="list" ' + style_border + ' width="">' + merchandise_info[2] + '</td>';
		merchandise_list_html = merchandise_list_html + '  <td class="list" ' + style_border + ' width="100"><input type="test" name="merchandise_price" id="merchandise_price_' + _sn + '" sn="' + _sn + '" value="0" onpropertychange="get_total(this)" style="width:94px; text-align:center;"  /></td>';
		merchandise_list_html = merchandise_list_html + '  <td class="list" ' + style_border + ' width="100"><input type="test" name="merchandise_number" id="merchandise_number_' + _sn + '" sn="' + _sn + '" value="0" onpropertychange="get_total(this)" style="width:94px; text-align:center;" /></td>';
		merchandise_list_html = merchandise_list_html + '  <td class="list" ' + style_border + ' width="50">' + merchandise_info[3] + '</td>';
		merchandise_list_html = merchandise_list_html + '  <td class="list" ' + style_border + ' width="100"><input type="test" name="merchandise_total" id="merchandise_total_' + _sn + '" value="0" onpropertychange="get_all_total()"  style="width:94px; text-align:center;" readonly="readOnly"  /></td>';
		merchandise_list_html = merchandise_list_html + '  <td class="list" ' + style_border + ' width="40"><input type="button" onclick="del_merchandise(this)" value="删" /></td>';
		merchandise_list_html = merchandise_list_html + '</tr>';
		_sn++;
		$('#merchandise_list').append(merchandise_list_html);
	}
	if(merchandise_list.length>0){
		add_foot(is_less);
	}
}
function add_foot(is_less){
	var foot = document.getElementById('foot');
	if(is_less&&null==foot){
		var merchandise_list_html = '';
		merchandise_list_html = merchandise_list_html + '<tr id="foot">';
		merchandise_list_html = merchandise_list_html + '  <td style="border-bottom:0; border-top:1px solid #888;" class="list" width="200">&nbsp;</td>';
		merchandise_list_html = merchandise_list_html + '  <td style="border-bottom:0; border-top:1px solid #888;" class="list" width="">&nbsp;</td>';
		merchandise_list_html = merchandise_list_html + '  <td style="border-bottom:0; border-top:1px solid #888;" class="list" width="100">&nbsp;</td>';
		merchandise_list_html = merchandise_list_html + '  <td style="border-bottom:0; border-top:1px solid #888;" class="list" width="100">&nbsp;</td>';
		merchandise_list_html = merchandise_list_html + '  <td style="border-bottom:0; border-top:1px solid #888;" class="list" width="50">&nbsp;</td>';
		merchandise_list_html = merchandise_list_html + '  <td style="border-bottom:0; border-top:1px solid #888;" class="list" width="100">&nbsp;</td>';
		merchandise_list_html = merchandise_list_html + '  <td style="border-bottom:0; border-top:1px solid #888;" class="list" width="40">&nbsp;</td>';
		merchandise_list_html = merchandise_list_html + '</tr>';
		$('#merchandise_list').append(merchandise_list_html);
	}
}
function del_merchandise(obj){
	var container = obj.parentNode.parentNode;
	container.removeNode(true);
	_max_length++;
	var is_less = 0-_max_length<=_max;
	add_foot(is_less);
}
function get_total(obj){
	var sn = obj.sn;
	var price = $('#merchandise_price_'+sn).val();
	var number = $('#merchandise_number_'+sn).val();
	var total = price * number;
	$('#merchandise_total_'+sn).val(total);
}
function get_all_total(){
	var totals = document.getElementsByName('merchandise_total');
	var all_total = 0;
	for(var i=0;i<totals.length;i++){
		all_total = all_total - totals[i].value;
	}
	$('#invoice_total').val(0 - all_total);
}